<template>
  <BaseDialog
    :title="(!props.item.commonId ? '新增' : '编辑') + '常见问题'"
    :handleCancel="handleCancel"
    :dialogVisible="props.dialogVisible"
  >
    <BaseForm
      labelWidth="100px"
      type="rule"
      :handleOk="handleOk"
      :handleCancel="handleCancel"
      :items="formItems"
      v-model:form="ruleForm"
      :rules="rules"
      :refresh="handleSearch"
    >
    </BaseForm>
  </BaseDialog>
</template>

<script setup lang="ts">
import { contentManagementService } from '@/api/contentManagement'
import store from '@/store'
const cateList = computed(() => store.getters.cateList)
const props = defineProps({
  dialogVisible: {
    type: Boolean,
  },
  item: {
    type: Object,
  },
  handleSearch: {
    type: Function,
  },
  searchParams: {
    type: Object,
  },
})
const emit = defineEmits(['update:dialogVisible'])
const formItems = ref([
  {
    name: '所属分类',
    key: 'typeId',
    type: 'select',
    options: cateList,
  },
  {
    name: '关联业务',
    key: 'business',
    type: 'select',
    options: [
      {
        label: '查询停电信息',
        value: 1,
      },
      {
        label: '查询电费',
        value: 2,
      },
      {
        label: '投诉建议',
        value: 3,
      },
      {
        label: '填写信息',
        value: 4,
      },
    ],
  },
  
  {
    name: '问题',
    key: 'question',
    type: 'input',
    maxLength: 20,
  },
  {
    name: '回答',
    key: 'answer',
    maxLength: 20,

    type: 'textarea',
    maxlength: '500',
    'show-word-limit': true,
  },
  {
    name: '状态',
    key: 'isDisable',
    type: 'switch',
    activeValue: 0,
    inactiveValue: 1,
  },
])
const rules = reactive({
  typeId: [{ required: true, message: '请选择', trigger: 'change' }],
  question: [{ required: true, message: '请输入', trigger: 'change' }],
  answer: [{ required: true, message: '请输入', trigger: 'change' }],
  isDisable: [{ required: true, message: '请选择', trigger: 'change' }],
})
const ruleForm = ref({
  commonId: '',
  typeId: '',
  question: '',
  answer: '',
  isDisable: 0,
})
const handleOk = () => {
  if (props.item.commonId) {
    return contentManagementService.libCommonUpdate({
      data: { ...ruleForm.value,business:ruleForm.value.business?ruleForm.value.business:0 },
    })
  } else {
    return contentManagementService.libCommonSaveBatch({
      data: [{ ...ruleForm.value ,business:ruleForm.value.business?ruleForm.value.business:0}],
    })
  }
}
const handleCancel = () => {
  emit('update:dialogVisible', false)
}
onMounted(() => {
  if (props.item.commonId) {
    ruleForm.value.typeId = props.item.typeId
    ruleForm.value.question = props.item.question
    ruleForm.value.answer = props.item.answer
    ruleForm.value.isDisable = props.item.isDisable
    ruleForm.value.commonId = props.item.commonId
    ruleForm.value.business=props.item.business?props.item.business:undefined
  } else {
    ruleForm.value.typeId = props.searchParams.typeId
  }
})
</script>

<style lang="scss" scoped></style>
